﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Organization;

namespace School.Pages.Organization
{
    public partial class Stuff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var xx = (IQueryable<int>)Session["privilage"];

                if (xx.Contains(1307) == true)
                {

                    if (!IsPostBack)
                    {
                        ddlBind(ddCamp, School.Service.Bus.GetAllCampuses().ToList());
                        LstManagers.DataSource = Staff.All().Select(x => new { Id = x.Id, Name = x.FName + " " + x.MName + " " + x.LName }); ;
                        LstManagers.DataValueField = "Id";
                        LstManagers.DataTextField = "Name";
                        LstManagers.DataBind();

                        LstManagers.Items.Insert(0, new ListItem() { Value = "0", Text = " " });

                        LstRoles.DataSource = Staff.Roles();
                        LstRoles.DataValueField = "Id";
                        LstRoles.DataTextField = "Name";
                        LstRoles.DataBind();
                        LstDepartments.DataSource = Department.All();
                        LstDepartments.DataValueField = "Id";
                        LstDepartments.DataTextField = "Name";
                        LstDepartments.DataBind();
                    }

                    GridView1.DataSource = Staff.All().Select(x => new { Id = x.Id, Name = x.FName + " " + x.MName + " " + x.LName, SDepartment = x.ORG_Department.Name, SRole = x.ORG_Role.Name });
                    GridView1.DataBind();
                    lbltst.Visible = false;
                    long id = 0;
                    if (long.TryParse(Request.QueryString["Id"], out id) && !IsPostBack)
                    {
                        var s = Staff.Single(id);
                        bool re = false;
                        if (s != null)
                        {
                            TxtFName.Text = s.FName;
                            TxtMName.Text = s.MName;
                            TxtLName.Text = s.LName;
                            TxtPhone.Text = s.PhoneNumber;
                            TxtAddress.Text = s.HomeAddress;
                            if (s.LeaveDate != null)
                                TxtleaveDate.Text = DateTime.Parse(s.LeaveDate.ToString()).ToShortDateString();
                            TxtJoinDate.Text = s.JoinDate.ToShortDateString();

                            if (s.Manager_Id != null)
                                LstManagers.SelectedValue = s.Manager_Id.ToString();

                            LstDepartments.SelectedValue = s.Department_Id.ToString();
                            LstRoles.SelectedValue = s.Role_Id.ToString();
                            RIsActive.SelectedValue = s.IsActvie.ToString();
                            TxtUsername.Text = s.UserName;
                            txtpass.Text = s.UPassword;
                            txtpassconf.Text = s.UPassword;
                            if (s.Campus_Id != null)
                                ddCamp.SelectedValue = s.Campus_Id.ToString();
                            re = Staff.Getstuf_byId(id);
                            if (re)
                                rdbteacher.SelectedValue = "0";
                            else
                                rdbteacher.SelectedValue = "1";
                        }
                        else
                        {
                            Response.Redirect("Stuff.aspx");

                        }
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            ORG_Stuff s = new ORG_Stuff();

            s.FName = TxtFName.Text;
            s.MName = TxtMName.Text;
            s.LName = TxtLName.Text;

            if (LstManagers.SelectedValue != "0")
                s.Manager_Id = long.Parse(LstManagers.SelectedValue);

            s.Department_Id = int.Parse(LstDepartments.SelectedValue);
            s.Role_Id = long.Parse(LstRoles.SelectedValue);
            if (TxtleaveDate.Text != "")
                s.LeaveDate = DateTime.Parse(TxtleaveDate.Text);
            s.JoinDate = DateTime.Parse(TxtJoinDate.Text);
            s.PhoneNumber = TxtPhone.Text;
            s.HomeAddress = TxtAddress.Text;
            s.IsActvie = bool.Parse(RIsActive.SelectedValue);
            s.UserName = TxtUsername.Text;
            s.UPassword = txtpass.Text;
            s.Campus_Id = Convert.ToInt32(ddCamp.SelectedValue);


            ORG_Stuff_Teacher staft = new ORG_Stuff_Teacher();
            staft.Certificate_Id = null;
            staft.Grade_ID = null;
            staft.Stage_Id = null;

            long id = 0;
            bool re = false;
            bool res = false;
            if (long.TryParse(Request.QueryString["Id"], out id))
            {
                s.Id = id;
                Staff.Update(s);
                staft.Stuff_Id = id;
                if (rdbteacher.SelectedValue == "0")
                {
                    res = Staff.Getstuf_byId(id);
                    if (res == false)
                        Staff.Add_ORG_Stuff_Teacher(staft);
                    re = true;
                }
                else if (rdbteacher.SelectedValue == "1")
                {
                    res = Staff.Getstuf_byId(id);
                    if (res)
                        re = Staff.DeleteStaff(id);
                }
            }
            else
            {
                staft.Stuff_Id = Staff.Add(s);
                if (rdbteacher.SelectedValue == "0")
                {
                    Staff.Add_ORG_Stuff_Teacher(staft);
                    re = true;
                }
            }
            if (re)
                Response.Redirect("Stuff.aspx");
            else
            {
                lbltst.Visible = true;
                lbltst.Text = "لا يمكن تغيير المدرس";
            }
        }
    }
}